package com.example.day09_event;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;

public class MyViewGroup extends LinearLayout {
    String TAG = "MyViewGroup";
    private float downX;
    private float downY;

    public MyViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /*
    分发事件的方法
    */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.d(TAG, "dispatchTouchEvent: "+ev.getAction());
        //super.dispatchTouchEvent(ev);//分发事件
        //true:事件被消费
        //false:事件返回给父容器/activity的onTouchEvent
        return super.dispatchTouchEvent(ev);
    }

    /*
    拦截事件的方法
    */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.d(TAG, "onInterceptTouchEvent: "+ev.getAction());
        //super.onInterceptTouchEvent(ev);//事件分发
        //true: 事件拦截,事件给到自己的onTouchEvent()
        //false:和返回super.一样
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                downX = ev.getX();
                downY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float moveX = ev.getX();
                float moveY = ev.getY();

                float dx = moveX - downX;
                float dy = moveY - downY;
                if (Math.abs(dx) < Math.abs(dy)) {
                    //垂直
                    return true;
                }
        }
        return super.onInterceptTouchEvent(ev);
    }
    /*
    处理事件方法
    */

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d(TAG, "onTouchEvent: "+event.getAction());
        return true;
    }
}
